﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_Join : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TrainID.Value = Request.Params["tid"];
            FetchCountry();
            ///
            // 初始化
            ///
            CmsDataContext db = new CmsDataContext();
            
            var px2 = (from aA in db.Article where aA.Verified != null orderby aA.Verified descending select aA).Take(6);
            Repeater3.DataSource = px2;
            Repeater3.DataBind();
        }
    }

    private void FetchCountry()
    {
        Country.Items.Clear();

        XmlDataSource xds = new XmlDataSource();
        xds.DataFile = Toolkit.GetRealDataFilePath("Street.xml");
        xds.XPath = "/Root/Country";

        Country.DataSource = xds;
        Country.DataTextField = "name";
        Country.DataValueField = "name";
        Country.DataBind();

        Country.Items.Insert(0, new ListItem("请选择", ""));

        Street.Items.Clear();
        Street.Items.Insert(0, new ListItem("请选择", ""));

        Village.Items.Clear();
        Village.Items.Insert(0, new ListItem("请选择", ""));
    }

    private void FetchStreet()
    {
        Street.Items.Clear();

        if (Country.SelectedValue != "")
        {
            XmlDataSource xds = new XmlDataSource();
            xds.DataFile = Toolkit.GetRealDataFilePath("Street.xml");
            xds.XPath = string.Format("/Root/Country[@name='{0}']/Street", Country.SelectedValue);

            Street.DataSource = xds;
            Street.DataTextField = "name";
            Street.DataValueField = "name";
            Street.DataBind();
        }

        Street.Items.Insert(0, new ListItem("请选择", ""));

        Village.Items.Clear();
        Village.Items.Insert(0, new ListItem("请选择", ""));
    }

    private void FetchVillage()
    {
        Village.Items.Clear();

        if (Street.SelectedValue != "")
        {
            XmlDataSource xds = new XmlDataSource();
            xds.DataFile = Toolkit.GetRealDataFilePath("Street.xml");
            xds.XPath = string.Format("/Root/Country[@name='{0}']/Street[@name='{1}']/Village", Country.SelectedValue, Street.SelectedValue);

            Village.DataSource = xds;
            Village.DataTextField = "name";
            Village.DataValueField = "name";
            Village.DataBind();
        }

        Village.Items.Insert(0, new ListItem("请选择", ""));
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();

        try
        {
            JoinTrain jt = new JoinTrain();
            jt.ID = Toolkit.NextUUID;
            jt.TrainID = TrainID.Value.Trim();
            jt.Name = Name.Text.Trim();
            jt.Phone = Telphone.Text.Trim();
            jt.Age = Convert.ToInt32(Age.Text.Trim());
            jt.Memo = Memo.Text.Trim();
            jt.Country = Country.SelectedValue.Trim();
            jt.Street = Street.SelectedValue.Trim();
            jt.Village = Village.SelectedValue.Trim();
            
            db.JoinTrain.InsertOnSubmit(jt);
            db.SubmitChanges();

            Response.Write("<script>alert('您已成功登记培训。');document.location='./Index.aspx';</script>");
        }
        catch (Exception ee)
        {
            Response.Write("<script>alert('登记信息时发生错误（" + ee.Message + "），请稍后重新尝试登记。');document.location='./Index.aspx';</script>");
        }
    }
    protected void Country_SelectedIndexChanged(object sender, EventArgs e)
    {
        FetchStreet();
    }
    protected void Street_SelectedIndexChanged(object sender, EventArgs e)
    {
        FetchVillage();
    }

}